package com.doctor.windflower_doctor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;

import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.DoctorGroupBeen;
import com.doctor.windflower_doctor.entity.NotificationBeen;
import com.doctor.windflower_doctor.service.DialogueService;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.LogUtils;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class SystemApplication extends Application {
	private static SystemApplication instance;
	public Context mContext;
	public Activity mCurrentActivity;
	public ImageLoader mImageLoad1;
	public static BitmapUtils mImageLoad;
	public boolean isTypeChange = true;
	private SharedPreferencesDB sharedPreferencesDB;
	public List<DoctorGroupBeen> doctorGroupBeens;
	public List<NotificationBeen> notificationBeens = new ArrayList<NotificationBeen>();
	public List<NotificationBeen> notifications = new ArrayList<NotificationBeen>();
	public final File file = new File(Environment.getExternalStorageDirectory() + File.separator + "com.doctor.windflower_doctor" + File.separator
			+ "windflower_doctor.apk");
	public int replyStatus = 0;
	public String doctorId;

	public static SystemApplication getInstance() {
		return instance;
	}

	@Override
	public void onCreate() {

		instance = this;
		LogUtils.setDEBUG(true);
		mContext = getApplicationContext();
		// 初始化图片加载器
		initImageLoad();
		ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPriority(Thread.NORM_PRIORITY - 2)// 设置线程的优先级
				.denyCacheImageMultipleSizesInMemory()// 当同一个Uri获取不同大小的图片，缓存到内存时，只缓存一个。默认会缓存多个不同的大小的相同图片
				.discCacheFileNameGenerator(new Md5FileNameGenerator())// 设置缓存文件的名字
				.discCacheFileCount(50)// 缓存文件的最大个数
				.tasksProcessingOrder(QueueProcessingType.LIFO)// 设置图片下载和显示的工作队列排序
				.build();
		sharedPreferencesDB = SharedPreferencesDB.getInstance(mContext);
		if (sharedPreferencesDB.getIsLogin())
			rigistSoket();
		ImageLoader.getInstance().init(configuration);
		mImageLoad1 = ImageLoader.getInstance();
		deleteFilesByDirectory(file);
		super.onCreate();
	}

	private void initImageLoad() {
		mImageLoad = new BitmapUtils(this, Constent.IMAGE_CARCH_PATH);
		mImageLoad.configDiskCacheEnabled(true);
		mImageLoad.configMemoryCacheEnabled(true);
	}

	public void rigistSoket() {
		mContext.startService(new Intent(this, DialogueService.class));
	}

	public void stopSoket() {
		mContext.stopService(new Intent(this, DialogueService.class));
	}

	/**
	 * 判断当前应用程序处于前台还是后台
	 */
	public boolean isApplicationBroughtToBackground(final Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (!tasks.isEmpty()) {
			ComponentName topActivity = tasks.get(0).topActivity;
			if (!topActivity.getPackageName().equals(context.getPackageName())) {
				return true;
			}
		}
		return false;

	}

	private void deleteFilesByDirectory(File directory) {
		LogUtils.e("=========================开始删除=============1================");
		if (directory.isFile() && directory.exists()) {
			LogUtils.e("=========================开始删除===================2==========");
			directory.delete();
		}
	}

}
